package net.skyedu.service.administrator;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.skyedu.bean.Departments;
import net.skyedu.bean.ShowUser;
import net.skyedu.bean.SumLate;
import net.skyedu.dao.UserInfoDao;
import net.skyedu.utils.Page;
import net.skyedu.utils.PageHelp;

public class Admins extends HttpServlet {

	private static final long serialVersionUID = 5952060001106180351L;

	/**
	 * Constructor of the object.
	 */
	public Admins() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		
		String schID=request.getParameter("schID");
		String State=request.getParameter("state");
		int sta=Integer.parseInt(request.getParameter("sta"));
		String name=request.getParameter("username");
		String startTime=request.getParameter("startTime");
		String endTime=request.getParameter("endTime");
		String pagerMethod=request.getParameter("pagerMethod");
		
		if (pagerMethod != null) {
			startTime = (String)session.getAttribute("startTime");
			endTime = (String)session.getAttribute("endTime");
			name=(String)session.getAttribute("nam");
		}
		int status=0;
		if(State!=null){
			status=Integer.parseInt(State);
			session.setAttribute("state",status);
		}else{
			Integer ss=(Integer)session.getAttribute("state");
			if(ss!=null)
				status=ss;			
		}
		int schId=0;
		if(schID!=null){
			schId=Integer.parseInt(schID);
			session.setAttribute("schId",schId);
		}else{
			Integer sch=(Integer)session.getAttribute("schId");
			if(sch!=null)
				schId=sch;			
		}
		UserInfoDao userDao=new UserInfoDao();
		List<Departments> depali=userDao.getAllSch();
//		System.out.println(startTime+"-----"+endTime);
		int totalRows=userDao.getCountBySchID(schId,name,startTime, endTime,status,sta);
		Page page=PageHelp.getPager(request, totalRows);
		List<ShowUser> attli=userDao.getAttBySchId(page.getPageSize(), page.getStartRow(), schId,name,startTime, endTime,status,sta);
		SumLate sum=userDao.getSum(name,startTime,endTime,sta);
		
		request.setAttribute("hrsum", sum);
		System.out.println(sum.getLateSum());
		request.setAttribute("page", page);
		request.setAttribute("depali", depali);
		request.setAttribute("attli", attli);
		request.setAttribute("con", sta);
		session.setAttribute("startTime",startTime);
		session.setAttribute("endTime",endTime);
		session.setAttribute("nam",name);
		request.getRequestDispatcher("attend/admin/admin.jsp").forward(request, response);
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
